package org.jeecg.modules.travel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.travel.code.CheckStatusCode;
import org.jeecg.modules.travel.dto.noticeBar.NoticeBarAddDto;
import org.jeecg.modules.travel.dto.noticeBar.NoticeBarAuthDto;
import org.jeecg.modules.travel.dto.noticeBar.NoticeBarEditDto;
import org.jeecg.modules.travel.entity.JhNoticeBar;
import org.jeecg.modules.travel.mapper.JhNoticeBarMapper;
import org.jeecg.modules.travel.service.IJhNoticeBarService;
import org.jeecg.modules.travel.vo.noticeBar.AppNoticeBarRecordVo;
import org.jeecg.modules.travel.vo.noticeBar.PcNoticeBarRecordVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class JhNoticeBarServiceImpl extends ServiceImpl<JhNoticeBarMapper, JhNoticeBar> implements IJhNoticeBarService {


    @Override
    public IPage<PcNoticeBarRecordVo> queryPage(Page<JhNoticeBar> page, QueryWrapper<JhNoticeBar> queryWrapper) {
        return baseMapper.queryPage(page, queryWrapper);
    }

    @Override
    public String noticeBarAdd(NoticeBarAddDto dto) {
        JhNoticeBar noticeBar = new JhNoticeBar();
        BeanUtils.copyProperties(dto, noticeBar);
        boolean flag = save(noticeBar);
        if (!flag) {
            return "添加失败";
        }
        return null;
    }

    @Override
    public String noticeBarEdit(NoticeBarEditDto dto) {
        JhNoticeBar  noticeBar = getById(dto.getId());
        if (noticeBar == null) {
            return "通知公告不存在";
        }
        UpdateWrapper<JhNoticeBar> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(JhNoticeBar::getId,dto.getId())
                .set(JhNoticeBar::getNoticeName,dto.getNoticeName())
                .set(JhNoticeBar::getNoticeContent,dto.getNoticeContent())
                .set(JhNoticeBar::getSortNo,dto.getSortNo());
        boolean flag = update(updateWrapper);
        if (!flag) {
            return "编辑失败";
        }
        return null;
    }

    @Override
    public String noticeBarAuth(NoticeBarAuthDto dto, String userId) {
        JhNoticeBar noticeBar = getById(dto.getId());
        if (noticeBar == null) {
            return "通知公告不存在";
        }
        UpdateWrapper<JhNoticeBar> wrapper = new UpdateWrapper<>();
        wrapper.lambda().set(JhNoticeBar::getCheckStatus, dto.getCheckStatus())
                .set(JhNoticeBar::getCheckPeople, userId)
                .set(JhNoticeBar::getCheckTime, new Date())
                .set(JhNoticeBar::getCheckRemark, Objects.equals(dto.getCheckStatus(), CheckStatusCode.NOT_APPROVED)
                        ? dto.getCheckRemark() : null)
                .eq(JhNoticeBar::getId, dto.getId());
        boolean flag = update(wrapper);
        if (!flag) {
            return "审核失败";
        }
        return null;
    }

    @Override
    public IPage<AppNoticeBarRecordVo> appQueryList(Page<JhNoticeBar> page, QueryWrapper<JhNoticeBar> queryWrapper) {
        return baseMapper.appQueryList(page,queryWrapper);
    }

}
